package cn.stylefeng.guns.modular.system.service.impl;


import cn.stylefeng.guns.modular.system.dto.DeviceRecordQuery;
import cn.stylefeng.guns.modular.system.model.DeviceRecord;
import cn.stylefeng.guns.modular.system.repository.DeviceRecordRepository;
import cn.stylefeng.guns.modular.system.service.IDeviceRecordService;

import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DeviceRecordServiceImpl implements IDeviceRecordService {
    @Autowired
    private DeviceRecordRepository deviceRecordRepository;

    @Override
    public Optional<DeviceRecord> selectById(String deviceRecordId) {
        return deviceRecordRepository.findById(deviceRecordId);

    }

    @Override
    public Object selectList(DeviceRecordQuery o) {
        List<DeviceRecord> deviceRecordList = new ArrayList<>();
        if (StringUtils.isNoneBlank(o.getDeviceid()) && StringUtils.isNoneBlank(o.getAction())) {
            deviceRecordList = deviceRecordRepository.findAllByDeviceidAndAction(o.getDeviceid(), o.getAction());
        } else if (StringUtils.isBlank(o.getDeviceid()) && StringUtils.isNotBlank(o.getAction())) {
            deviceRecordList = deviceRecordRepository.findAllByAction(o.getAction());
        } else if (StringUtils.isNoneBlank(o.getDeviceid())) {
            deviceRecordList = deviceRecordRepository.findAllByDeviceid(o.getDeviceid());
        } else if (o.getStartDate() != null && o.getStartDate().equals(o.getEndDate())) {
            deviceRecordList = deviceRecordRepository.findAllByDate(o.getStartDate());
        } else {
            deviceRecordList = deviceRecordRepository.findAll();
        }

        if (o.getStartDate() == null) {
            o.setStartDate((long) 0);
        }
        if (o.getEndDate() == null) {
            o.setEndDate(Long.MAX_VALUE);
        }

        deviceRecordList = deviceRecordList.stream().filter(deviceRecord -> deviceRecord.getDate() >= o.getStartDate() && deviceRecord.getDate() <= o.getEndDate()).sorted(Comparator.comparing(DeviceRecord::getDate)).collect(Collectors.toList());
        return deviceRecordList;
    }

    @Override
    public void insert(DeviceRecord deviceRecord) {
        deviceRecordRepository.insert(deviceRecord);

    }

    @Override
    public void deleteById(String deviceRecordId) {
        deviceRecordRepository.deleteById(deviceRecordId);
    }

    @Override
    public void updateById(DeviceRecord deviceRecord) {
        deviceRecordRepository.save(deviceRecord);
    }

    @Override
    public org.springframework.data.domain.Page<DeviceRecord> getDeviceRecords(Page page, DeviceRecordQuery o) {
        int offset =page.getOffset();
        int limit =page.getLimit();
        Sort sort = null;
        if(o.getSort()!=null) {
            if(o.getOrder().equals("desc")){
                sort = new Sort(Sort.Direction.DESC, o.getSort());
            }else{
                sort = new Sort(Sort.Direction.ASC, o.getSort());
            }
        }
        Pageable pageable = PageRequest.of(offset%limit==0?offset/limit:offset/limit+1, limit);
        if(sort!=null){
            pageable = PageRequest.of(offset%limit==0?offset/limit:offset/limit+1, limit,sort);
        }


        org.springframework.data.domain.Page<DeviceRecord> pageResult=null;
        if (o.getStartDate() == null) {
            o.setStartDate((long) 0);
        }
        if (o.getEndDate() == null) {
            o.setEndDate((long) Integer.MAX_VALUE);
        }

        if(StringUtils.isNoneBlank(o.getDeviceid()) && StringUtils.isNoneBlank(o.getAction()) && o.getStartDate() != null && o.getStartDate().equals(o.getEndDate())){
            pageResult = deviceRecordRepository.findAllByDeviceidAndActionAndDate(o.getDeviceid(),o.getAction(),o.getStartDate(),pageable);
        }

        else if (StringUtils.isNoneBlank(o.getDeviceid()) && StringUtils.isNoneBlank(o.getAction())) {
            pageResult = deviceRecordRepository.findAllByDeviceidAndActionAndDateBetween(o.getDeviceid(), o.getAction(),o.getStartDate()-1,o.getEndDate()+1,pageable);
        } else if (StringUtils.isBlank(o.getDeviceid()) && StringUtils.isNotBlank(o.getAction())) {
            pageResult = deviceRecordRepository.findAllByActionAndDateBetween(o.getAction(),o.getStartDate()-1,o.getEndDate()+1,pageable);
        } else if (StringUtils.isNoneBlank(o.getDeviceid())&& StringUtils.isBlank(o.getAction())) {
            pageResult = deviceRecordRepository.findAllByDeviceidAndDateBetween(o.getDeviceid(),o.getStartDate()-1,o.getEndDate()+1,pageable);
        } else if (o.getStartDate() != null && o.getStartDate().equals(o.getEndDate())) {
            pageResult = deviceRecordRepository.findAllByDate(o.getStartDate(),pageable);
        } else {
            pageResult = deviceRecordRepository.findAllByDateBetween(o.getStartDate()-1,o.getEndDate()+1,pageable);
        }


        return pageResult;
    }
}
